import Vue from 'vue'
import { axios } from '@/utils/request'
// import { localityAxios } from '@/utils/request'
import MessageUtil from '../utils/MessageUtil'
import YchConstant from '@/api/YchConstant'
import LocaleUtil from "../utils/LocaleUtil";

const api = {
  user: '/api/user',
  role: '/api/role',
  service: '/api/service',
  permission: '/api/permission',
  permissionNoPager: '/api/permission/no-pager',
  getEnableSmsValidate:YchConstant.SPZX_SPCY+'/loginSms/getEnableSmsValidate',
  getSmsCode:YchConstant.SPZX_SPCY+'/loginSms/getSmsCode',
  validateLoginCode:YchConstant.SPZX_SPCY+'/loginSms/validateLoginCode',

  //在线编辑文档--服务端
  getWebOfficeDownloadFile:YchConstant.SPZX_SPCY + '/webOfficeApi/getWebOfficeDownloadFile',//保存和关闭

  openFile:YchConstant.SPZX_SPCY + '/webOfficeApi/openFile',//打开
  openFileReadOnly:YchConstant.SPZX_SPCY + '/webOfficeApi/openFileReadOnly',//查看
  getDocxToPdf:YchConstant.SPZX_SPCY + '/webOfficeApi/getDocxToPdf',//保存成PDF
  againEditPdfOnlineFile:YchConstant.SPZX_SPCY + '/webOfficeApi/againEditPdfOnlineFile',//重新编辑
  onlineTypeSwitch:YchConstant.SPZX_SPCY + '/webOfficeApi/onlineTypeSwitch',//编辑模式
  getDocxToPdfAndGz:YchConstant.SPZX_SPCY + '/webOfficeApi/getDocxToPdfAndGz',//保存成pdf并盖章
  pdfFileHcCa:YchConstant.SPZX_SPCY + '/gxca/pdfFileHcCa',//盖章

  wjmbWebOfficeApiOpenFile:YchConstant.SPZX_SPCY + '/wjmbWebOfficeApi/openFile',//打开模板文档
  ZltxwjWebOfficeApiGetWebOfficeDownloadFile:YchConstant.SPZX_SPCY + '/ZltxwjWebOfficeApi/getWebOfficeDownloadFile',//质量体系文件保存和关闭
  ZltxwjWebOfficeApiOpenFile:YchConstant.SPZX_SPCY + '/ZltxwjWebOfficeApi/openFile',//打开质量体系文件
  ZltxwjWebOfficeApiOpenFileBase:YchConstant.SPZX_SPCY + '/ZltxwjWebOfficeApi/openFileBase',//打开质量体系文件
  wjmbWebOfficeApiGetWebOfficeDownloadFile:YchConstant.SPZX_SPCY + '/wjmbWebOfficeApi/getWebOfficeDownloadFile',//模板文档关闭在线
  queryYwflWjmbList:YchConstant.SPZX_SPCY + '/spcyRsWjmb/queryYwflWjmbList',
  selectWjmb:YchConstant.SPZX_SPCY + '/common/selectWjmb',//选择模板

  //未读消息提示
  sysMessList:YchConstant.SPZX_OA + '/sysTzMessage/list',//用户通知信息表-分页列表查询
  getMessage:YchConstant.SPZX_OA + '/sysTzMessage/getMessage',//通过id已读自己的信息通知
  getTzMessage:YchConstant.SPZX_OA + '/sysTzMessage/getTzMessage',//批量已读

  queryTopMenuList:YchConstant.SPZX_SPCY + '/menu/queryTopMenuList',//查询用户顶级权限菜单
  spcyMyInfoverifyByYkhQzdmTx:YchConstant.SPZX_SPCY + '/spcyMyInfo/verifyByYkhQzdmTx',//验证登录用户-银行卡-电子签名-头像是否存在
  jcylxdjcjuploadMyQm:YchConstant.SPZX_SPCY + '/jcylxdjcj/uploadMyQm',//上传签名
  usergetCurrentUserDeparts:YchConstant.PREFIX_SYSTEM + "/user/getCurrentUserDeparts",//查询部门
  myJbxxUpdateUser:YchConstant.SPZX_SPCY + '/myJbxx/updateUser',//保存个人信息

  guideCzzyBxdZt:YchConstant.SPZX_SPCY + '/guide/czzyBxdZt',// 获取操作指引-审评员报销单-操作状态
  yssbSfcw:YchConstant.SPZX_OA + '/yssb/sfcw',// 是否财务
  yssbSfcn:YchConstant.SPZX_OA + '/yssb/sfcn',// 是否出纳
}

export default api


//post
export function postAction(url,parameter) {
  return axios({
    url: url,
    method:'post' ,
    data: parameter
  })
}


//post method= {post | put}
export function httpAction(url,parameter,method) {
  return axios({
    url: url,
    method:method ,
    data: parameter
  })
}

//获取验证码
export function getEnableSmsValidate(data){
  return axios({
    url: api.getEnableSmsValidate,
    method:'get' ,
    params:data,
    url_1:'outside'
  })
}
//发送验证码--并返回秒数
export function getSmsCode(data){
  return axios({
    url: api.getSmsCode,
    method:'post' ,
    data:data,
    url_1:'outside'
  })
}
export function queryTopMenuList(data){
  return axios({
    url: api.queryTopMenuList,
    method:'get' ,
    params:data,
    url_1:'outside'
  })
}

export function spcyMyInfoverifyByYkhQzdmTx(data){
  return axios({
    url: api.spcyMyInfoverifyByYkhQzdmTx,
    method:'get' ,
    params:data,
    url_1:'outside'
  })
}
export function jcylxdjcjuploadMyQm(params){
  return axios({
    url: api.jcylxdjcjuploadMyQm,
    method:'post' ,
    data:params,
    url_1:'outside',
  })
}
export function myJbxxUpdateUser(params){
  return axios({
    url: api.myJbxxUpdateUser,
    method:'post' ,
    data:params,
    url_1:'outside',
  })
}

export function usergetCurrentUserDeparts(params){
  return axios({
    url: api.usergetCurrentUserDeparts,
    method:'get' ,
    params:params,
  })
}

export function guideCzzyBxdZt(data){
    return axios({
        url: api.guideCzzyBxdZt,
        method:'get' ,
        params:data,
        url_1:'outside'
    })
}

// 是否财务
export function yssbSfcw(params){
    return axios({
        url: api.yssbSfcw,
        method:'get' ,
        params:{...params},
        url_2:'outside',
    })
}

// 是否出纳
export function yssbSfcn(params){
    return axios({
        url: api.yssbSfcn,
        method:'get' ,
        params:{...params},
        url_2:'outside',
    })
}



//验证验证码
export function validateLoginCode(data){
  return axios({
    url: api.validateLoginCode,
    method:'get' ,
    params:data,
    url_1:'outside'
  })
}

//put
export function putAction(url,parameter) {
  return axios({
    url: url,
    method:'put',
    data: parameter
  })
}

//get
export function getAction(url,parameter) {
  return axios({
    url: url,
    method: 'get',
    params: parameter
  })
}

//deleteAction
export function deleteAction(url,parameter) {
  return axios({
    url: url,
    method: 'delete',
    params: parameter
  })
}

export function getUserList(parameter) {
  return axios({
    url: api.user,
    method: 'get',
    params: parameter
  })
}

export function getRoleList(parameter) {
  return axios({
    url: api.role,
    method: 'get',
    params: parameter
  })
}

export function getServiceList(parameter) {
  return axios({
    url: api.service,
    method: 'get',
    params: parameter
  })
}

export function getPermissions(parameter) {
  return axios({
    url: api.permissionNoPager,
    method: 'get',
    params: parameter
  })
}

// id == 0 add     post
// id != 0 update  put
export function saveService(parameter) {
  return axios({
    url: api.service,
    method: parameter.id == 0 ? 'post' : 'put',
    data: parameter
  })
}

/**
 * 下载文件 用于excel导出
 * @param url
 * @param parameter
 * @returns {*}
 */
export function downFile(url,parameter, method){
  return axios({
    url: url,
    params: (!method || method.toLowerCase() == "get") ? parameter : {},
    data: (method && method.toLowerCase() == "post") ? parameter : {},
    method: method || 'get',
    responseType: 'blob'
  })
}

/**
 * 下载文件
 * @param url 文件路径
 * @param fileName 文件名
 * @param parameter
 * @returns {*}
 */
export function downloadFile(url, fileName, parameter, method) {
  return downFile(url, parameter, method).then((data) => {
    if (!data || data.size === 0) {
      MessageUtil.warning(LocaleUtil.getLocale('m.eform.formDesign.label.wen-jian-xia-zai-shi-bai'))
      return
    }
    if (typeof window.navigator.msSaveBlob !== 'undefined') {
      window.navigator.msSaveBlob(new Blob([data]), fileName)
    } else {
      let url = window.URL.createObjectURL(new Blob([data]))
      let link = document.createElement('a')
      link.style.display = 'none'
      link.href = url
      link.setAttribute('download', fileName)
      document.body.appendChild(link)
      link.click()
      document.body.removeChild(link) //下载完成移除元素
      window.URL.revokeObjectURL(url) //释放掉blob对象
    }
  })
}

/**
 * 文件上传 用于富文本上传图片
 * @param url
 * @param parameter
 * @returns {*}
 */
export function uploadAction(url,parameter){
  return axios({
    url: url,
    data: parameter,
    method:'post' ,
    headers: {
      'Content-Type': 'multipart/form-data',  // 文件上传
    },
  })
}

/**
 * 获取文件服务访问路径
 * @param avatar
 * @returns {*}
 */
export function getFileAccessHttpUrl(avatar) {
  if (avatar && (avatar.startsWith('http') || avatar.startsWith('https'))) {
    return avatar;
  } else {
    if (avatar && avatar.length > 0 && avatar.indexOf('[') === -1 && avatar !== 'null') {
      // 统一从nginx或vue.config.js中转发，减少后台开放接口
      // let baseUrl = location.protocol + "//" + location.host
      // return baseUrl + "/api/system/common/static/" + avatar;
      const staticUrl = bootConfig[process.env.NODE_ENV].VUE_APP_STATIC_URL
      if (staticUrl && !avatar.startsWith(staticUrl)) {
        return staticUrl + "/" + avatar;
      }
    }
  }
  return avatar
}

export function uidGenerator(){
  return '-' + parseInt(Math.random() * 10000 + 1, 10);
}
export function getFileName(path){
  if (path.lastIndexOf("\\") >= 0) {
    let reg = new RegExp("\\\\", "g");
    path = path.replace(reg, "/");
  }
  return path.substring(path.lastIndexOf("/") + 1);
}

/**
 * TODO 判断是否允许当前环境编辑设计器脚本
 * 用于防止线上环境的一些危险操作被使用
 * @param noWarning 不允许编辑时不显示提示信息，返回reject
 * true 标识允许； false 标识禁止
 * @returns {Promise<unknown>}
 */
export function allowEditScript(noWarning) {
  return new Promise((resolve,reject) => {
    getAction(YchConstant.PREFIX_SYSTEM + '/permission/allowEditScript').then(res => {
      if (res.result === true) {
        resolve()
      } else {
        if(noWarning){
          reject()
        }else{
          MessageUtil.warning(LocaleUtil.getLocale('m.common.tip.disabled_current_action'))
        }

      }
    })
  })
}



//在线编辑文档接口--服务
export function getWebOfficeDownloadFile(parameter) {
  return axios({
    url: api.getWebOfficeDownloadFile,
    method: 'get',
    params: parameter,
    url_1:'outside'
  })
}
export function ZltxwjWebOfficeApiGetWebOfficeDownloadFile(parameter) {
  return axios({
    url: api.ZltxwjWebOfficeApiGetWebOfficeDownloadFile,
    method: 'get',
    params: parameter,
    url_1:'outside'
  })
}

export function wjmbWebOfficeApiGetWebOfficeDownloadFile(parameter) {
  return axios({
    url: api.wjmbWebOfficeApiGetWebOfficeDownloadFile,
    method: 'get',
    params: parameter,
    url_1:'outside'
  })
}
export function openFile(parameter) {
  return axios({
    url: api.openFile,
    method: 'get',
    params: parameter,
    url_1:'outside'
  })
}

export function openFileReadOnly(parameter) {
  return axios({
    url: api.openFileReadOnly,
    method: 'get',
    params: parameter,
    url_1:'outside'
  })
}
export function wjmbWebOfficeApiOpenFile(parameter) {
  return axios({
    url: api.wjmbWebOfficeApiOpenFile,
    method: 'get',
    params: parameter,
    url_1:'outside'
  })
}

export function ZltxwjWebOfficeApiOpenFileBase(parameter) {
  return axios({
    url: api.ZltxwjWebOfficeApiOpenFileBase,
    method: 'get',
    params: parameter,
    url_1:'outside'
  })
}
export function ZltxwjWebOfficeApiOpenFile(parameter) {
  return axios({
    url: api.ZltxwjWebOfficeApiOpenFile,
    method: 'get',
    params: parameter,
    url_1:'outside'
  })
}
export function getDocxToPdf(parameter) {
  return axios({
    url: api.getDocxToPdf,
    method: 'get',
    params: parameter,
    url_1:'outside'
  })
}
export function againEditPdfOnlineFile(parameter) {
  return axios({
    url: api.againEditPdfOnlineFile,
    method: 'get',
    params: parameter,
    url_1:'outside'
  })
}
export function onlineTypeSwitch(parameter) {
  return axios({
    url: api.onlineTypeSwitch,
    method: 'get',
    params: parameter,
    url_1:'outside'
  })
}


export function getDocxToPdfAndGz(parameter) {
  return axios({
    url: api.getDocxToPdfAndGz,
    method: 'get',
    params: parameter,
    url_1:'outside'
  })
}

export function pdfFileHcCa(parameter) {
  return axios({
    url: api.pdfFileHcCa,
    method: 'get',
    params: parameter,
    url_1:'outside'
  })
}

// OA未读消息提示
export function sysMessList(parameter) {
  return axios({
    url: api.sysMessList,
    method: 'get',
    params: parameter,
    url_2:'outside'
  })
}
// 通过id已读自己的信息通知
export function getMessage(parameter) {
  return axios({
    url: api.getMessage,
    method: 'get',
    params: parameter,
    url_2:'outside'
  })
}
// 批量已读
export function getTzMessage(parameter) {
  return axios({
    url: api.getTzMessage,
    method: 'get',
    params: parameter,
    url_2:'outside'
  })
}





